package com.yufa.springcloud.controller;

import com.netflix.discovery.EurekaClient;
import com.yufa.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderConstroller {

    @Value("${user.url}")
    private String url;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private EurekaClient client;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping(value = "/order2/{id}")
    private User fromClient(@PathVariable Long id) {
        ResponseEntity<User> forEntity = restTemplate.getForEntity("http://PROVIDER-USER/user/" + id, User.class);
        return forEntity.getBody();
    }
    @GetMapping(value = "/test")
    private String test() {
        ServiceInstance choose = loadBalancerClient.choose("PROVIDER-USER");
        System.out.println(choose.getInstanceId()+choose.getHost()+choose.getPort());
        return "随机";
    }
    @GetMapping(value = "/test2")
    private String test2() {
        ServiceInstance choose = loadBalancerClient.choose("PROVIDER-USER2");
        System.out.println(choose.getInstanceId()+choose.getHost()+choose.getPort());
        return "轮询";
    }
}
